package com.glela.order.service.impl.order;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import com.glela.common.constant.BaseRestrictionsEnum;
import com.glela.common.service.SMSService;
import com.glela.common.util.JsonUtil;
import com.glela.common.util.StringUtil;
import com.glela.order.model.OrderInfo;
import com.glela.order.model.OrderObject;
import com.glela.order.model.OrderSettlementCommision;
import com.glela.order.service.OrderInfoService;
import com.glela.order.service.OrderService;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.model.OperationLog;
import com.glela.platform.model.response.ResponseUser;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.service.OperationLogService;
import com.glela.platform.service.UserService;

/**
 * **********************************************
 * @Title: 下单发送短信监听队列
 * @throws @author gaoweiqi
 * @date 2017年01月22日 下午5:20:00
 * **********************************************
 */
public class SMSPayOrder implements MessageListener {
	private static Logger logger = LoggerFactory.getLogger(SMSPayOrder.class);
	private BaseRestrictionsService baseRestrictionsService;
	private UserService userService;
	private OrderService orderService2;
	private SMSService smsService;
	private OrderInfoService orderInfoService;
	private OperationLogService operationLogService;

	public void onMessage(Message message) {
		OrderSettlementCommision temp = null;
		String orderThing = null;
		try {
			// 转换消息实体
			orderThing = new String(message.getBody());
			temp = JsonUtil.toBean(orderThing, OrderSettlementCommision.class);
		} catch (Exception e) {
			logger.error("下单发送短信接收消息错误：{}-{}-{}", orderThing,
					message.getBody(), e.getMessage());
		}
		logger.error("下单发送短信:{}",temp.getOrderSn());
		try {
			OrderObject objorder = orderService2.selectOrderObjectByOrderSn(temp.getOrderSn());
			if (objorder.getOpenStoreUserId() != null) {// 有店中店用户 判断是不是店主  如果是店主就发短息
				ResponseUser OpenStoreUser = userService.selectUserRoleById(objorder.getOpenStoreUserId());
				if (OpenStoreUser != null&& ArrayUtils.contains(new int[] {3,4,7,11},OpenStoreUser.getRoleId())) {// 是店主 发短信
					ResponseUser PayOrderUser = userService.selectUserRoleById(objorder.getUserId());
					if (PayOrderUser != null&& !StringUtils.isAnyBlank(PayOrderUser.getUserName(),OpenStoreUser.getUserName())) {// 准备短信内容
						BaseRestrictions baseRestrictions = baseRestrictionsService.getBaseRestrictionsForCache(
								BaseRestrictionsEnum.PAY_ORDER_SMS.getText());
						if (baseRestrictions != null&& baseRestrictions.getStatus() == 1) {// 需要发送短信
							List<OrderInfo> listorderinfo = orderInfoService.selectOrderInfoByOrderObjectIdC(objorder.getId());
							for (OrderInfo orderInfo : listorderinfo) {
								// PayOrderPhone在你的小铺下单啦！订单编号:orderSn【小铺来了】
								String SMS = baseRestrictions.getName().replace(
									"PayOrderPhone",StringUtil.replaceChars(3, 2,PayOrderUser.getUserName()))
									.replace("orderSn",orderInfo.getOrderSn())
								.replace("amount",String.valueOf((orderInfo.getCommisionFee()+orderInfo.getPointFee())/100.0));
								Long responseSMS = smsService.sendMsgJianZhou(
										true, OpenStoreUser.getUserName(), SMS);// 立即发送短信
								if (responseSMS < 0) {
									logger.error("下单发送短信失败：下单人：{}，短信接收人:{}",
											PayOrderUser.getUserName(),
											OpenStoreUser.getUserName());
								}
								// 短信日志
								OperationLog log = new OperationLog();
								log.setObjId(OpenStoreUser.getUserId().intValue());
								log.setObjName(String.valueOf(OpenStoreUser.getNickName()));
								log.setModule("PayOrderCallOpenStoreUser");
								log.setOperateType("sendSms");
								Long createLongTime = ((new Date().getTime()) / 1000);
								log.setCreatedAt(createLongTime.intValue());
								log.setDescription("下单短信提醒" + SMS);
								operationLogService.saveOperateLog(log);
							}
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error("下单发送短信队列处理失败：{}-{}", orderThing, e.getMessage());
		}
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setOrderService2(OrderService orderService2) {
		this.orderService2 = orderService2;
	}

	public void setBaseRestrictionsService(
			BaseRestrictionsService baseRestrictionsService) {
		this.baseRestrictionsService = baseRestrictionsService;
	}

	public void setSmsService(SMSService smsService) {
		this.smsService = smsService;
	}

	public void setOrderInfoService(OrderInfoService orderInfoService) {
		this.orderInfoService = orderInfoService;
	}

	public void setOperationLogService(OperationLogService operationLogService) {
		this.operationLogService = operationLogService;
	}
}
